//
//  YZEFaBiaoViewController.m
//  yilutongxing
//
//  Created by ios-23 on 17/4/6.
//  Copyright © 2017年 YZE. All rights reserved.
//

#import "YZEFaBiaoViewController.h"
#import "YYTextView.h"
#import <ActionSheetCustomPicker.h>
#import <ActionSheetDatePicker.h>
//#import "LGPhoto.h"
#import "DzyImgPicker.h"

#define GraftingsURL @"/api/graftings"
#define URL_Topics @"/api/topics"

@interface YZEFaBiaoViewController ()<YYTextViewDelegate,DzyImgDelegate,ActionSheetCustomPickerDelegate,NetworkUtilsDelegate>
@property(nonatomic,strong)DzyImgPicker *DzyView;
@property (nonatomic ) NSMutableArray *data;

@property (nonatomic,strong) ActionSheetCustomPicker *hospital_picker; // 选择器
@property(nonatomic,strong)NSArray *typeArr;
@property (nonatomic,assign) NSInteger index1; // 下标
@property (nonatomic,strong) NSArray *selections; //!< 选择的三个下标

@property (weak, nonatomic) IBOutlet UIView *selectView;
@property (weak, nonatomic) IBOutlet YYTextView *titileTF;
@property (weak, nonatomic) IBOutlet YYTextView *detailTV;
@property (nonatomic, strong) YYTextView *currentTV;
@property (weak, nonatomic) IBOutlet UILabel *typeLable;

@property (nonatomic, strong) NSMutableArray *selectedPhotos;
@property (nonatomic, copy) NSString *grafthingId;
@end

@implementation YZEFaBiaoViewController
//键盘回收

- (IBAction)tapForSelectedType:(UITapGestureRecognizer *)sender {
    self.hospital_picker=[[ActionSheetCustomPicker alloc]initWithTitle:nil delegate:self showCancelButton:YES origin:self.view initialSelections:@[@(self.index1)]];
    
    self.hospital_picker.tapDismissAction  = TapActionSuccess;
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, 44, 44);
    [button setTitle:@"取消" forState:UIControlStateNormal];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    button1.frame = CGRectMake(0, 0, 44, 44);
    [button1 setTitle:@"确定" forState:UIControlStateNormal];
    [self.hospital_picker setCancelButton:[[UIBarButtonItem alloc] initWithCustomView:button]];
    [self.hospital_picker setDoneButton:[[UIBarButtonItem alloc] initWithCustomView:button1]];
    
    [self.hospital_picker showActionSheetPicker];
}

//代理方式返回数组
- (void)getBase64Images:(NSArray *)imgData{
    self.selectedPhotos = [imgData mutableCopy];
     NSLog(@"imgData == %@",imgData);
}

- (void)textViewDidBeginEditing:(YYTextView *)textView{
    self.currentTV = textView;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //从相册返回的话需要刷新下界面
    [_DzyView.collectionView reloadData];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.grafthingId = @"1";
    self.title=@"新话题";
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"发布" style:UIBarButtonItemStylePlain target:self action:@selector(fabu)];

    UIImage *leftImage=[[UIImage imageNamed:@"返回"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *leftButtonItem=[[UIBarButtonItem alloc]initWithImage:leftImage style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem=leftButtonItem;
    
    [self layoutDzyView];
//获取所有移植类型
    [[YZENetwork sharedInstace]getRequestWithUrl:[BASE stringByAppendingString:GraftingsURL] parameters:nil delegate:self requestTag:1 successBlock:^(id responseObject) {
        self.typeArr = [BaseModel mj_keyValuesArrayWithObjectArrayAdd:responseObject];
    }];
}
-(void)back{
    [self dismissViewControllerAnimated:YES completion:nil];
}

//发布按钮响应
-(void)fabu{
    if (self.titileTF.text.length==0){
        UIAlertController *alter = [UIAlertController alertControllerWithTitle:@"" message:@"标题不能为空" preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [self dismissViewControllerAnimated:YES completion:nil];
            
        }];
        [alter addAction:action];
        [self presentViewController:alter animated:YES completion:nil];
    }
    if (self.detailTV.text.length==0){
        UIAlertController *alter = [UIAlertController alertControllerWithTitle:@"" message:@"内容不能为空" preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [self dismissViewControllerAnimated:YES completion:nil];
            
        }];
        [alter addAction:action];
        [self presentViewController:alter animated:YES completion:nil];
    }
    //处理图片
    if (self.selectedPhotos.count>0) {
        NSDictionary *dic=@{@"name":self.titileTF.text,@"content":self.detailTV.text,@"graftingId":self.grafthingId,@"images":self.selectedPhotos};
        [self sendMesssageWithDataDic:dic];
    }else{
        NSDictionary *dic=@{@"name":self.titileTF.text,@"content":self.detailTV.text,@"graftingId":self.grafthingId};

        [self sendMesssageWithDataDic:dic];
    }
}
-(void)sendMesssageWithDataDic:(NSDictionary*)dic{
        [[YZENetwork sharedInstace] postRequestWithUrl:[NSString stringWithFormat:@"%@%@?%@", BASE,URL_Topics,PLATFORM] parameters:dic delegate:self requestTag:2 successBlock:^(id responseObject) {
            UIAlertController *alter = [UIAlertController alertControllerWithTitle:@"" message:@"发布成功" preferredStyle:UIAlertControllerStyleActionSheet];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [self dismissViewControllerAnimated:YES completion:nil];
            }];
            [alter addAction:action];
            [self presentViewController:alter animated:YES completion:nil];
        }];
}
-(void)layoutDzyView{
    //设置选择器
    if (self.selections.count) {
        self.index1 = [self.selections[0] integerValue];
    }
    self.data = [NSMutableArray array];
    //此处需要注意  自己计算一下  我设置的每个cell 是60*60  间距10 所以 这里一般是设置 全屏宽度  如有特殊需求自行修改
    
    self.DzyView = [[DzyImgPicker alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH,220) andParentV:self andMaxNum:6];
    self.DzyView.dzyImgDelegate = self;
    [self.selectView addSubview:self.DzyView];
    
    __weak typeof(self)weakSelf = self;
    [self.DzyView setDzyImgs:^(NSArray *data) {
        NSLog(@"block --- %lu",(unsigned long)data.count);
        weakSelf.data = [data mutableCopy];
        if (weakSelf.data.count>=4) {
            weakSelf.DzyView.collectionView.frame=CGRectMake(0, 0, SCREEN_WIDTH, 220);
        }
        if (weakSelf.data.count<4) {
            weakSelf.DzyView.collectionView.frame=CGRectMake(0, 0, SCREEN_WIDTH, 110);
        }
    }];
}
#pragma mark - UIPickerViewDataSource Implementation
//返回pickerview列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
//返回pickerview某列行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.typeArr.count;
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel* label = (UILabel*)view;
    if (!label)
    {
        label = [[UILabel alloc] init];
        [label setFont:[UIFont systemFontOfSize:20]];
    }
    
    label.textAlignment = NSTextAlignmentCenter;
    
    label.text = self.typeArr[row][@"name"];
    return label;
}
- (void)configurePickerView:(UIPickerView *)pickerView
{
    pickerView.showsSelectionIndicator = NO;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    self.index1=row;
}
// 点击done的时候回调
- (void)actionSheetPickerDidSucceed:(ActionSheetCustomPicker *)actionSheetPicker origin:(id)origin
{
    self.typeLable.text=self.typeArr[self.index1][@"name"];
    self.grafthingId = self.typeArr[self.index1][@"id"];
}
#pragma mark - 网络代理
- (void)OnFail:(NSString *)errorMsg errorCode:(NSInteger)errorCode requestTag:(int)tag{
    if (tag==2) {
        [UIView showToastWindowWithMessage:errorMsg];
    }
}

@end
